package net.helix.engine.library;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import javax.swing.tree.TreePath;

import net.helix.engine.library.items.LibraryItem;

public class LibraryTreeNode{
	/**
	 * An id which links to the library item's arrayList
	 */
	public int id;
	
	private Library library;
	
	private String name;
	private String nameLower;
	
	public static final int FOLDER = -1;
	
	private LibraryTreeNode parent = null;
	
	public ArrayList<LibraryTreeNode> children;
	
	/**
	 * Creates a libraryTreeNode that links to the id in the library. When displayed
	 * in the library UI, the name and type is fetched from the id.
	 * @param id
	 */
	
	public LibraryTreeNode(){
		
	}
	public LibraryTreeNode(Library library, int id, String name){
		this.library = library;
		this.id = id;
		setName(name);
		if(id == FOLDER){
			children = new ArrayList<LibraryTreeNode>();
		}
	}
	public void addNode(LibraryTreeNode node){
		if(id == FOLDER){
			System.out.println("Adding node "+node.name);
			children.add(node);
			node.parent = this;
		}else{
			System.out.println("Error, cannot add this to a non-folder!");
		}
		
	}
	public void addNodeTo(int index, LibraryTreeNode node){
		if(id == FOLDER){
			System.out.println("Adding node "+node.name);
			children.add(index, node);
			node.parent = this;
		}else{
			System.out.println("Error, cannot add this to a non-folder!");
		}
		
	}
	public int getIndexOfChild(LibraryTreeNode node){
		if(id == FOLDER){
			return children.indexOf(node);
		}else{
			return -1;
		}
	}
	public void removeNode(LibraryTreeNode node){
		if(id == FOLDER){
			System.out.println("Removing node "+node.name);
			children.remove(node);
			node.parent = null;
		}else{
			System.out.println("Error, cannot add this to a non-folder!");
		}
	}
	
	public void rename(String newName){
		library.getItem(id).name = newName;
	}
	public void setName(String newName){
		this.name = newName;
		this.nameLower = newName.toLowerCase();
	}
	public String getName(boolean lowerCase){
		if(lowerCase){
			return nameLower;
		}else{
			return name;
		}
	}
	public String getName(){
		return name;
	}
	public int getChildCount(){
		if(id == FOLDER){
			return children.size();
		}else{
			return 0;
		}
	}
	public LibraryTreeNode getParent(){
		return parent;
	}
	private LibraryTreeNode[] parentList;
	private int depth;
	public TreePath getPathTo(){
		parentList = null;
		depth = 0;
		
		System.out.println("Making tree path ");
		_getPathTo(this);
		
		
		TreePath path = new TreePath(parentList);
		System.out.println("done making tree path");
		
		return path;
	}
	private void _getPathTo(LibraryTreeNode node){
		depth++;
		if(node.getParent() != null){
			_getPathTo(node.getParent());
		}else{
			parentList = new LibraryTreeNode[depth];
		}
		depth--;
		System.out.println("node: "+node.getName());
		parentList[(parentList.length - 1) - depth] = node;
	}
	
	public LibraryItem getItem(){
		if(id == FOLDER){
			return null;
		}else{
			return library.getItem(id);
		}
	}
	
	/**
	 * Will attempt to free the memory used by the item because this node is 
	 * being deleted from the library.
	 */
	public void dispose(){
		if(id != FOLDER){
			getItem().dispose();
		}
	}
	
	public Library getLibrary(){
		return library;
	}
	
	
}
